package com.xqgl.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.xqgl.bean.Activity;
import com.xqgl.dao.ActivityDao;
import com.xqgl.util.ConnertionUtil;

public class ActivityDaoImp implements ActivityDao{
	Connection conn = ConnertionUtil.getConnection();
	PreparedStatement ps;
	Statement statement;
	
	public ArrayList<Activity> getAllUserInfos() {
		ArrayList<Activity> activitys = new ArrayList<>();
		String sql = "select * from activity ";
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rst = ps.executeQuery(sql);
			rst.last();
			rst.beforeFirst();
			while(rst.next()) {
				Activity activity = new Activity();
				activity.setId(rst.getInt("id"));
				activity.setVillagename(rst.getString("villagename"));
				activity.setActitle(rst.getString("actitle"));
				activity.setVenue(rst.getString("venue"));
				activity.setAccontent(rst.getString("accontent"));
				activity.setOrganizer(rst.getString("organizer"));
				activity.setAcstime(rst.getString("acstime"));
				activity.setAcdeadline(rst.getString("acdeadline"));
				activity.setAcctime(rst.getString("acctime"));
				activity.setAcstate(rst.getString("acstate"));
				activitys.add(activity);
			}		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return activitys;
	}
	
	@Override
	public int add(Activity activity) throws SQLException {
		String sql = "insert into activity(villagename,actitle,venue,accontent,organizer,acstime,acdeadline,acctime,acstate)"
				+ "values(?,?,?,?,?,?,?,?,?)";
		ps = conn.prepareStatement(sql);
		ps.setString(1, activity.getVillagename());
		ps.setString(2, activity.getActitle());
		ps.setString(3, activity.getVenue());
		ps.setString(4, activity.getAccontent());
		ps.setString(5, activity.getOrganizer());
		ps.setString(6, activity.getAcstime());
		ps.setString(7, activity.getAcdeadline());
		ps.setString(8, activity.getAcctime());
		ps.setString(9, activity.getAcstate());
		int a = ps.executeUpdate();
		ps.close();
		return a;
	}
	@Override
	public int update(Activity activity) throws SQLException {
		String sql = "update activity set villagename = '"+activity.getVillagename()+"', actitle = '"+activity.getActitle()+"', venue = '"+activity.getVenue()+"', accontent = '"+activity.getAccontent()+"', organizer = '"+activity.getOrganizer()+"',"
				+ "acstime = '"+activity.getAcstime()+"', acdeadline = '"+activity.getAcdeadline()+"', acctime = '"+activity.getAcctime()+"', acstate = '"+activity.getAcstate()+"' where id = "+activity.getId()+"";
		ps = conn.prepareStatement(sql);
		int b = ps.executeUpdate();
		ps.close();
		return b;
	}
	@Override
	public boolean delete(int id) throws SQLException {
		String sql = "delete from activity where id = "+id+"";
		statement = conn.createStatement();
		boolean c = statement.execute(sql);
		statement.close();
		return !c;
	}
	@Override
	public Activity findById(int id) throws SQLException {
		Activity activity = new Activity();
		String sql = "select * from activity where id = ?";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet set = ps.executeQuery();
		while(set.next()){
			activity.setId(set.getInt("id"));
			activity.setVillagename(set.getString("villagename"));
			activity.setActitle(set.getString("actitle"));
			activity.setVenue(set.getString("venue"));
			activity.setAccontent(set.getString("accontent"));
			activity.setOrganizer(set.getString("organizer"));
			activity.setAcstime(set.getString("acstime"));
			activity.setAcdeadline(set.getString("acdeadline"));
//			activity.setAcctime(set.getString("acctime"));
			activity.setAcstate(set.getString("acstate"));
		}
		ps.close();
		return activity;
	}
}
